/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.az.ducbt.cliphot;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import org.json.me.JSONException;
import org.json.me.JSONObject;

/**
 *
 * @author Administrator
 */
public class JsonParser {

    private Constant ct = new Constant();
    private String jsonData = null;

    public JsonParser() {
    }

    public JSONObject getJsonDataFromUrl(String url) throws IOException, JSONException {
        StringBuffer stringBuffer = new StringBuffer();
        InputStream is = null;
        HttpConnection hc = null;
        JSONObject jObj = null;
        //System.out.println(url);
        try {
            hc = (HttpConnection) Connector.open(url, Connector.READ_WRITE, false);
            hc.setRequestProperty("User-Agent", "Profile/MIDP-2.0, Configuration/CLDC-1.0");
            hc.setRequestProperty("Content-Language", "en-US");
            hc.setRequestMethod(HttpConnection.GET);
            is = hc.openInputStream();
            int ch;
            while ((ch = is.read()) != -1) {
                stringBuffer.append((char) ch);
            }
            jsonData = stringBuffer.toString();
        } catch (Exception e) {
        }

        try {
            hc.close();
            is.close();
        } catch (Exception e) {
            //System.out.println("Error in MostActivePareser Connection close:" + e);
        }
        //System.out.println("jsonData: " + jsonData);
        try {
            jObj = new JSONObject(jsonData);
        } catch (JSONException e) {
        }
        return jObj;
    }

    private String readFile(String fileName) {
        InputStream is = getClass().getResourceAsStream(fileName);
        StringBuffer sb = new StringBuffer();
        try {
            int chars, i = 0;
            while ((chars = is.read()) != -1) {
                sb.append((char) chars);
            }
            return sb.toString();
        } catch (Exception e) {
        }
        return null;
    }
}
